(import
  (except (rnrs base) let-values map error)
  (only (guile)
        lambda* λ
        command-line
        make-typed-array)
  (srfi srfi-1)
  (fileio))


(define println
  (λ (thing)
    (simple-format (current-output-port) "~a\n" thing)))


(define lines->landscape
  (λ (lines)
    (list->array 2 (map string->list lines))))


(define main
  (λ (cmd-line-args)
    (let* ([lines (get-lines-from-file (second cmd-line-args))]
           [rows-step (string->number (third cmd-line-args))]
           [cols-step (string->number (fourth cmd-line-args))]
           [landscape (lines->landscape lines)])
      ;; assuming all lines have the same length
      (let* ([dim (array-dimensions landscape)]
             [height (first dim)]
             [width (second dim)])
        (let next-step ([cur-row-ind 0]
                        [cur-col-ind 0]
                        [encountered-trees 0])
          (cond
           [(>= cur-row-ind height) encountered-trees]
           [(char=? (array-ref landscape cur-row-ind cur-col-ind) #\#)
            (next-step (+ cur-row-ind rows-step)
                       (remainder (+ cur-col-ind cols-step) width)
                       (+ encountered-trees 1))]
           [else
            (next-step (+ cur-row-ind rows-step)
                       (remainder (+ cur-col-ind cols-step) width)
                       encountered-trees)]))))))


(simple-format (current-output-port)
               "~a\n"
               (main (command-line)))
